package greendbgui

import org.springframework.dao.DataIntegrityViolationException

class ElectoralRollEntryController 
{
    def electoralRollService

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() 
    {
//        log.error (params)
        
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        
        ElectoralRoll roll = ElectoralRoll.get (params.id)
        
        def entries = electoralRollService.getEntries (roll, params)
        
        [electoralRollEntryInstanceList: entries, 
         electoralRollEntryInstanceTotal: entries.totalCount]
    }

    // ?? this is exactly the same as list, because I get an error when the search button
    // attempts to invoke list. Shoudlget them both to call common code 
    def search() 
    {
//        log.error (params)
        
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        
        ElectoralRoll roll = ElectoralRoll.get (params.id)
        
        def entries = electoralRollService.getEntries (roll, params)
        
        render (view: "list",
        model:
        [electoralRollEntryInstanceList: entries, 
         electoralRollEntryInstanceTotal: entries.totalCount])
    }

    def create() {
        [electoralRollEntryInstance: new ElectoralRollEntry(params)]
    }

    def save() {
        def electoralRollEntryInstance = new ElectoralRollEntry(params)
        if (!electoralRollEntryInstance.save(flush: true)) {
            render(view: "create", model: [electoralRollEntryInstance: electoralRollEntryInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'electoralRollEntry.label', default: 'ElectoralRollEntry'), electoralRollEntryInstance.id])
        redirect(action: "show", id: electoralRollEntryInstance.id)
    }

    def show() {
        def electoralRollEntryInstance = ElectoralRollEntry.get(params.id)
        if (!electoralRollEntryInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'electoralRollEntry.label', default: 'ElectoralRollEntry'), params.id])
            redirect(action: "list")
            return
        }

        [electoralRollEntryInstance: electoralRollEntryInstance]
    }

    def edit() {
        def electoralRollEntryInstance = ElectoralRollEntry.get(params.id)
        if (!electoralRollEntryInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'electoralRollEntry.label', default: 'ElectoralRollEntry'), params.id])
            redirect(action: "list")
            return
        }

        [electoralRollEntryInstance: electoralRollEntryInstance]
    }

    def update() {
        def electoralRollEntryInstance = ElectoralRollEntry.get(params.id)
        if (!electoralRollEntryInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'electoralRollEntry.label', default: 'ElectoralRollEntry'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (electoralRollEntryInstance.version > version) {
                electoralRollEntryInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'electoralRollEntry.label', default: 'ElectoralRollEntry')] as Object[],
                          "Another user has updated this ElectoralRollEntry while you were editing")
                render(view: "edit", model: [electoralRollEntryInstance: electoralRollEntryInstance])
                return
            }
        }

        electoralRollEntryInstance.properties = params

        if (!electoralRollEntryInstance.save(flush: true)) {
            render(view: "edit", model: [electoralRollEntryInstance: electoralRollEntryInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'electoralRollEntry.label', default: 'ElectoralRollEntry'), electoralRollEntryInstance.id])
        redirect(action: "show", id: electoralRollEntryInstance.id)
    }

    def delete() {
        def electoralRollEntryInstance = ElectoralRollEntry.get(params.id)
        if (!electoralRollEntryInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'electoralRollEntry.label', default: 'ElectoralRollEntry'), params.id])
            redirect(action: "list")
            return
        }

        try {
            electoralRollEntryInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'electoralRollEntry.label', default: 'ElectoralRollEntry'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'electoralRollEntry.label', default: 'ElectoralRollEntry'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
